<template>
  <view class="coupon-page">
    <!-- 头部导航 -->
    <!-- <view class="header">
      <view class="back-btn" @click="goBack">
        <text class="back-icon">‹</text>
      </view>
      <text class="title">我的优惠券</text>
      <view class="test-btn" @click="toggleEmptyState">
        <text class="test-text">测试</text>
      </view>
    </view> -->
    <CustomNavBar 
        title="优惠券"
        backType="redirect"
        redirectUrl="/pages/me/me"
        />
    <!-- 优惠券列表 -->
    <view class="coupon-list" v-if="coupons.length > 0">
      <view
        class="coupon-item"
        v-for="(coupon, index) in coupons"
        :key="index"
        @click="goToDetail(coupon)"
      >
        <view class="coupon-left">
          <view class="coupon-type">
            <!-- <view class="type-indicator" :style="{backgroundColor: coupon.color}"></view> -->
            <text class="type-name">{{ coupon.type }}</text>
          </view>
          <view class="usage-progress">
            <view class="progress-label">
              <!-- <text class="progress-text">使用进度</text> -->
              <!-- <text class="progress-percentage">{{ coupon.usagePercent }}%</text> -->
            </view>
            <view class="progress-bar">
              <view class="progress-fill" :style="{width: coupon.usagePercent + '%'}"></view>
            </view>
          </view>
          <view class="coupon-info">
            <text class="usage-scope">适用范围：{{ coupon.scope }}</text>
            <text class="validity">有效期至{{ coupon.expiry }}</text>
          </view>
        </view>
        <view class="coupon-right">
          <view class="amount">
            <text class="currency">¥</text>
            <text class="value">{{ coupon.value }}</text>
            <text class="threshold">/¥{{ coupon.threshold }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-if="coupons.length == 0">
      <text class="empty-text">暂无可用优惠券</text>
      <view class="bottom-action">
        <view class="action-button" @click="">
          <image class="button-bg" src="https://tg-spcs.oss-cn-hangzhou.aliyuncs.com/static/images/settings/banner_little@2x.png" mode="aspectFill"></image>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import CustomNavBar from '@/components/CustomNavBar.vue'
// 优惠券数据
 //const coupons =ref([]);
const coupons = ref([
  {
    type: '洗车券',
    value: 100,
    threshold: 1000,
    scope: '与所辖合作商户',
    expiry: '2025-10-31',
    color: '#00D4AA',
    usagePercent: 70
  },
  {
    type: '停车券',
    value: 100,
    threshold: 1000,
    scope: '与所辖合作商户',
    expiry: '2025-10-31',
    color: '#00D4AA',
    usagePercent: 45
  },
  {
    type: '充电券',
    value: 100,
    threshold: 1000,
    scope: '与所辖合作商户',
    expiry: '2025-10-31',
    color: '#00D4AA',
    usagePercent: 100
  }
])

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}

// 跳转到详情页
const goToDetail = (coupon) => {
console.log('coupon', coupon.type)
  uni.navigateTo({
     url: '/pages/coupon-pack/detail?type='+coupon.type
  })
}

// 切换空状态（用于测试）
const toggleEmptyState = () => {
  if (coupons.value.length > 0) {
    coupons.value = []
  } else {
    coupons.value = [
       {
         type: '洗车券',
         value: 100,
         threshold: 1000,
         scope: '与所辖合作商户',
         expiry: '2025-10-31',
         color: '#00D4AA',
         usagePercent: 70
       },
       {
         type: '停车券',
         value: 100,
         threshold: 1000,
         scope: '与所辖合作商户',
         expiry: '2025-10-31',
         color: '#00D4AA',
         usagePercent: 45
       },
       {
         type: '充电券',
         value: 100,
         threshold: 1000,
         scope: '与所辖合作商户',
         expiry: '2025-10-31',
         color: '#00D4AA',
         usagePercent: 85
       }
     ]
  }
}

onMounted(() => {
  // 页面加载时可以调用API获取优惠券数据
  // fetchCoupons()
})
</script>

<style scoped>
.coupon-page {
  min-height: 100vh;
  background-color: #fff;
}

.header {
  display: flex;
  align-items: center;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-bottom: 1rpx solid #eee;
}

.back-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  font-size: 40rpx;
  color: #333;
  font-weight: bold;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 36rpx;
  font-weight: 500;
  color: #333;
}

.test-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #00D4AA;
  border-radius: 30rpx;
}

.test-text {
  font-size: 24rpx;
  color: #fff;
}

.coupon-list {
  padding: 30rpx;

}

.coupon-item {
  background: linear-gradient(to bottom, #fff, #E8F8F6 );
  border-radius: 20rpx;
  padding: 40rpx 30rpx;
  margin-bottom: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  height:190rpx;
  border: 1rpx solid #EBEBEB ;
}

.coupon-left {
  flex: 1;
}

.coupon-type {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
}

.type-indicator {
  width: 8rpx;
  height: 40rpx;
  border-radius: 4rpx;
  margin-right: 20rpx;
}

.type-name {
  font-size: 36rpx;
  font-weight: 540;
  color: #333;
}

.usage-progress {
  margin-bottom: 20rpx;
}

.progress-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10rpx;
}

.progress-text {
  font-size: 26rpx;
  color: #666;
}

.progress-percentage {
  font-size: 26rpx;
  color: #00C896;
  font-weight: 500;
}

.progress-bar {
  width: 90%;
  height: 8rpx;
  background-color: #F0F0F0;
  border-radius: 4rpx;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #10C2A9 0%, #1CCCB4 100%);
  border-radius: 4rpx;
  transition: width 0.3s ease;
}

.coupon-info {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.usage-scope,
.validity {
  font-size: 28rpx;
  color: #999;
}

.coupon-right {
  text-align: right;
}

.amount {
  display: flex;
  align-items: center;
  padding-bottom:30rpx;
}

.currency {
  font-size: 48rpx;
  color: #333;
  font-weight: 500;
}

.value {
  font-size: 48rpx;
  font-weight: 500;
  color: #333;
  margin: 0 5rpx;
}

.threshold {
  font-size: 30rpx;
  color: #999;
}

.empty-state {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 60vh;
}

.empty-text {
  font-size: 32rpx;
  color: #999;
  margin-bottom: 60rpx;
}

/* 底部按钮 */
.bottom-action {
  position: fixed;
  top: 85%;
  width: 95%;
}

.action-button {
  position: relative;
  height: 80px;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.button-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.button-content {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.button-title {
  font-size: 16px;
  font-weight: 600;
  color: #fff;
}

.button-subtitle {
  font-size: 12px;
  color: #fff;
  opacity: 0.9;
}
</style>
